package com.pan.cuiniubi.create;

import com.pan.cuiniubi.entity.Member;
import com.pan.cuiniubi.entity.Table;
import com.pan.cuiniubi.utils.ReadYamlTool;
import com.pan.cuiniubi.enumn.LaminaEnum;

import java.util.List;
import java.util.stream.Collectors;

public class CreateEntity {
    private static String entityTemplate;

    private static String lombokImportString;

    private static String lombokAnnotationString;

    public String packageStr;


    static {
        entityTemplate =
                "public class (0) {\n" +
                "(1)\n };";

        lombokImportString = "\nimport lombok.AllArgsConstructor;\n" +
                        "import lombok.Data;\n" +
                        "import lombok.NoArgsConstructor;\n" +
                        "import lombok.experimental.Accessors;\n";


        lombokAnnotationString = "@Data\n" +
                "@NoArgsConstructor\n" +
                "@AllArgsConstructor\n" +
                "@Accessors(chain = true)\n";
    }

    public String getMemberStr(Member member){
        String name = member.getMemberName();
        String type = member.getMemberType().getTypeName();
        String surplus = member.getSurplus();
        String result = "\t"+type+" "+name+"; //"+surplus+"\n";//拼接字符串
        return result;
    }

    public String getAllMemberStr(List<Member> memberList){
        String result = memberList.stream()
                .map(member-> getMemberStr(member))
                .reduce((x, y)->x+y).get();
        return result;
    }

    public String getPackageStr(){
        if (packageStr == null) {
            String path = (String) ReadYamlTool.getInstance().getMapLaminaValue(LaminaEnum.entity, "path");
            packageStr = "package " + path + ";\n";
        }
        return packageStr;
    }
    public String getTypeImportStr(List<Member> memberList){
        String result = memberList.stream().map(Member::getMemberType)
                .collect(Collectors.toSet()).stream()
                .map(type->"import "+type.getName()+";\n")
                .reduce((x,y)->x+y).get();
        return result;
    }

    public String getAnnotationStr(){
        return "";
    }

    public String getEntityString(Table table){
        return  getEntityString(table,true);
    }
    public String getEntityString(Table table,Boolean isLombok){
        String allMemberStr = getAllMemberStr(table.getMemberList());
        String tableName = table.getTableName();

        String packageStr = getPackageStr();

        String importStr = getTypeImportStr(table.getMemberList());
        if (isLombok){
            importStr += lombokImportString;
        }

        String annotationStr = getAnnotationStr();
        if (isLombok){
            annotationStr += lombokAnnotationString;
        }

        String result = entityTemplate;
        result = result.replace("(0)",tableName);
        result = result.replace("(1)",allMemberStr);

        result = packageStr+"\n"+importStr + "\n\n" + annotationStr + result;
        return result;
    }





}
